import Vue from 'vue'
import VueRouter from 'vue-router'

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push // 先存一份老的push方法
VueRouter.prototype.push = function push(location) {
  // 调用新的push 方法的时候，改变指向到老的
  return originalPush.call(this, location).catch(err => err)
}

import Login from '../views/user/Login.vue'
import Home from '../views/Home.vue'
import Welcome from '../views/Welcome.vue'
import Users from '../views/userControl/Users.vue'
import Rights from '../views/power/Rights.vue'
import Roles from '../views/power/Roles.vue'
import Cate from '../views/goods/Cate.vue'
import Params from '../views/goods/Params.vue'
import List from '../views/goods/List.vue'
import AddGoods from '../views/goods/Add.vue'
import Order from '../views/order/Order.vue'
import Report from '../views/report/Report.vue'
Vue.use(VueRouter)

const routes = [
  { path:'/', redirect:'/login' },
  { path: '/login', component: Login },
  { 
    path: '/home', 
    component: Home ,
    redirect:'/welcome',
    children:[
      {path:'/welcome',component:Welcome},
      {path:'/users',component:Users},
      {path:'/rights',component:Rights},
      {path:'/roles',component:Roles},
      {path:'/categories',component:Cate},
      {path:'/params',component:Params},
      {path:'/goods',component:List},
      {path:'/goods/add',component:AddGoods},
      {path:'/orders',component:Order},
      {path:'/reports',component:Report},
    ]
  },
]

const router = new VueRouter({
  routes
})


router.beforeEach((to,from,next)=>{
  if(to.path==='/login') return next();
  let token = sessionStorage.getItem('token');
  if(!token) return next('/login')
  next();
})


export default router
